package com.sanjiahantang.youjiarole.module.programmeprice.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.cases.models.ICaseModel;
import com.sanjiahantang.youjiarole.module.cases.models.impl.CaseModel;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.common.views.impl.WebActivity;
import com.sanjiahantang.youjiarole.module.goods.adapters.GoodsListMoreAdapter;
import com.sanjiahantang.youjiarole.module.goods.models.IGoodsModel;
import com.sanjiahantang.youjiarole.module.goods.models.impl.GoodsModel;
import com.sanjiahantang.youjiarole.module.match.beans.MatchBean;
import com.sanjiahantang.youjiarole.module.me.contract.models.IContractModel;
import com.sanjiahantang.youjiarole.module.me.contract.models.impl.ContractModel;
import com.sanjiahantang.youjiarole.module.me.order.models.IOrderModel;
import com.sanjiahantang.youjiarole.module.me.order.models.impl.OrderModel;
import com.sanjiahantang.youjiarole.module.programmeprice.adapters.ProgramAdapter;
import com.sanjiahantang.youjiarole.module.programmeprice.adapters.ProgramProjectAdapter;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramAddOrderListBean;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramBean;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramContractBean;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramOrderListBean;
import com.sanjiahantang.youjiarole.module.programmeprice.modles.IProgramPriceModel;
import com.sanjiahantang.youjiarole.module.programmeprice.modles.impl.ProgramPriceModel;
import com.sanjiahantang.youjiarole.module.programmeprice.views.ProgramPriceView;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/10/25.
 */

public class ProgramPricePresenter extends BaseMvpPresenter<ProgramPriceView> {
    private BaseView.OnLoadListener onLoadListener;
    private BaseView.OnLoadListener listener;
    private IProgramPriceModel programPriceModel;
    private ProgramBean.Data allData, commitData;
    private ProgramAdapter adapter;
    private ProgramProjectAdapter mAdapter;
    private IOrderModel orderModel;
    private IContractModel contractModel;
    private IGoodsModel goodsModel;
    private GoodsListMoreAdapter listMoreAdapter;
    private List<MatchBean.Goods> listData;
    private ICaseModel caseModel;

    public ProgramPricePresenter() {
        goodsModel=new GoodsModel();
        programPriceModel = new ProgramPriceModel();
        orderModel = new OrderModel();
        contractModel = new ContractModel();
        caseModel = new CaseModel();
        listData=new ArrayList<>();
    }

    //报价详情加载数据
    public void LoadBaoJList(Context context, String required_id,String version,int type) {
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put("ppId", required_id);
        map.put(KEY_VERSION,version);
        map.put("type", type);
        mView.showProgress();
        programPriceModel.OnLoadBaoJList(context, map, new CallBack<ProgramBean>() {
            @Override
            public void onSuccess(ProgramBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    if (response.getData() != null) {
                        if (response.getData()!=null){
                            adapter = new ProgramAdapter(context, onLoadListener);
                            adapter.setData(response.getData());
                            adapter.setChangeData("first");
                            Object[] obj = new Object[2];
                            obj[0] = CALLBACK_TYPE_RECYCLER_ADAPTER;
                            obj[1] = adapter;
                            onLoadListener.onLoad(obj);

                            Object[] objTitle = new Object[2];
                            objTitle[0] = CALLBACK_TYPE_STRING;
                            if (TextUtils.equals("1",response.getData().getPlanType())){
                                objTitle[1] = "清单报价";
                            }else{
                                objTitle[1] = "套餐报价";
                            }
                            onLoadListener.onLoad(objTitle);
                        }
                    }
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }

    public void setOnLoadListener(BaseView.OnLoadListener listener) {
        onLoadListener = listener;
    }

    //保存推送
    public void savePlanPriceInfo(Context context,int requeid,String ppid) {
        adapter.setChangeData("3");
        allData = new ProgramBean.Data();
        List<ProgramBean.Data.ServiceListBean> serviceList = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> materialList = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> furnitureList = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> accessoriesList = new ArrayList<>();

        for (int i = 0; i < adapter.getData().getServiceList().size(); i++) {
            serviceList.add(adapter.getData().getServiceList().get(i));
        }
        allData.setServiceList(serviceList);
        for (int i = 0; i < adapter.getData().getFurnitureList().size(); i++) {
            furnitureList.add(adapter.getData().getFurnitureList().get(i));
        }
        allData.setFurnitureList(furnitureList);
        for (int i = 0; i < adapter.getData().getMaterialList().size(); i++) {
                materialList.add(adapter.getData().getMaterialList().get(i));
        }
        allData.setMaterialList(materialList);
        for (int i = 0; i < adapter.getData().getAccessoriesList().size(); i++) {
                accessoriesList.add(adapter.getData().getAccessoriesList().get(i));
        }
        allData.setAccessoriesList(accessoriesList);
        allData.setToken(ConfigValue.DATA_KEY);
        allData.setRole(1);
        allData.setUserRequireId(requeid);
        allData.setPpId(Integer.valueOf(ppid));
        allData.setType(0);//0报价清单1增项
        mView.showProgress();
        programPriceModel.SavePricePlanInfo(context, allData, new CallBack<StringBean> () {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response.getCode()==RESPONSE_CODE_SUC){
                    Utils.showToast(context,"推送成功");
                    ((BaseMvpActivity)context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });

    }
    //增项报价清单
    public void addPriceLoadBaoJList(final Context context, String orderCode, final BaseView.OnLoadListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put("orderCode", orderCode);
        mView.showProgress();
        programPriceModel.addPriceOnLoadBaoJList(context, map, new CallBack<ProgramAddOrderListBean>() {
            @Override
            public void onSuccess(ProgramAddOrderListBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null) {
                    listener.onLoad(response.getData());
                } else {
                    Utils.showToast(context, "订单列表获取失败");
                    ((Activity) context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context, "订单列表获取失败");
                ((Activity) context).finish();
            }
        });
    }
    //项目管理保存推送
    public void saveProjectPriceInfo(Context context,int requeid,int ppid) {
        mAdapter.setChangeData("3");
        commitData= new ProgramBean.Data();
        List<ProgramBean.Data.ServiceListBean> serviceList = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> materialList = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> furnitureList = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> accessoriesList = new ArrayList<>();

        for (int i = 0; i < mAdapter.getData().getServiceList().size(); i++) {
            if (mAdapter.getData().getServiceList().get(i).getIsRecommend() == 1){
                serviceList.add(mAdapter.getData().getServiceList().get(i));
            }
        }
        commitData.setServiceList(serviceList);
        for (int i = 0; i < mAdapter.getData().getFurnitureList().size(); i++) {
            if (mAdapter.getData().getFurnitureList().get(i).getIsRecommend() == 1){
                furnitureList.add(mAdapter.getData().getFurnitureList().get(i));
            }

        }
        commitData.setFurnitureList(furnitureList);
        for (int i = 0; i < mAdapter.getData().getMaterialList().size(); i++) {
            if (mAdapter.getData().getMaterialList().get(i).getIsRecommend() == 1){
                materialList.add(mAdapter.getData().getMaterialList().get(i));
            }
        }
        commitData.setMaterialList(materialList);
        for (int i = 0; i < mAdapter.getData().getAccessoriesList().size(); i++) {
            if (mAdapter.getData().getAccessoriesList().get(i).getIsRecommend() == 1){
                accessoriesList.add(mAdapter.getData().getAccessoriesList().get(i));
            }
        }
        commitData.setAccessoriesList(accessoriesList);
        commitData.setToken(ConfigValue.DATA_KEY);
        commitData.setRole(3);
        commitData.setUserRequireId(requeid);
        commitData.setPpId(ppid);
        commitData.setType(1);//增项推送
        mView.showProgress();
        programPriceModel.SavePricePlanInfo(context, commitData, new CallBack<StringBean> () {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response.getCode()==RESPONSE_CODE_SUC){
                    Utils.showToast(context,"推送成功");
                    ((BaseMvpActivity)context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });

    }
    public void changData(Context context, String status) {
        adapter.setChangeData(status);
    }
    public void prochangData(Context context, String status) {
        mAdapter.setChangeData(status);
    }
    //项目管理加载数据工程
    public void findPlaceOrder(Context context, int ppId, int type) {
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put("ppId",ppId);
        map.put("type", type);
        map.put(KEY_VERSION,0);
        mView.showProgress();
        programPriceModel.findPlaceOrder(context,map,new CallBack<ProgramBean>() {
            @Override
            public void onSuccess(ProgramBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    if (response.getData() != null) {
                        mAdapter = new ProgramProjectAdapter(context, onLoadListener);
                        mAdapter.setData(response.getData());
                        mAdapter.setChangeData("1");
                        Object[] obj = new Object[2];
                        obj[0] = CALLBACK_TYPE_RECYCLER_ADAPTER;
                        obj[1] = mAdapter;
                        onLoadListener.onLoad(obj);
                    }

                    searchUserAddressOrder(context);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }
    //业务端order加载数据
    public void findPlanInfo(final Context context, int ppId, String version,final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("ppId",String.valueOf(ppId));
        if (version != null) param.put("version",version);
        mView.showProgress();
        programPriceModel.findPlanInfo(context,param, new CallBack<ProgramOrderListBean>() {
            @Override
            public void onSuccess(ProgramOrderListBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getData());
                }else{
                    Utils.showToast(context,"订单列表获取失败");
                    ((Activity)context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context,"订单列表获取失败");
                ((Activity)context).finish();
            }
        });
    }
    private void searchUserAddressOrder(Context context){
//        Map<String,String> params = new HashMap<>();
//        params.put(KEY_TOKEN,ConfigValue.DATA_KEY);
//        params.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
//        mView.showProgress();
//        orderModel.searchUserAddressOrder(context,params, new CallBack<AddressBean>() {
//            @Override
//            public void onSuccess(AddressBean response, Context context, Object tag) {
//                if (mView == null) return;
//                mView.hideProgress();
//                if (response.getCode() == RESPONSE_CODE_SUC && response.getData().size() > 0){
//                    Object[] obj = new Object[2];
//                    obj[0] = CALLBACK_TYPE_ADDRESS;
//                    obj[1] = response.getData().get(0);
//                    onLoadListener.onLoad(obj);
//                }
//
//            }
//
//            @Override
//            public void onFailure(String message) {
//                if (mView == null) return;
//                mView.hideProgress();
//            }
//        });
    }
    //提交订单 不需要
    public void creatOrderPhoneForPlan(Context context, ProgramBean.Data.OrderInvoiceInfoDtoBean data, String id,String message) {
        commitData.setToken(ConfigValue.DATA_KEY);
        commitData.setUserAddressId(34);
        commitData.setOrderInvoiceInfoDto(data);
        commitData.setPhoneTypeId(1);
        commitData.setBuyPersonMessage(message);
        programPriceModel.creatOrderPhoneForPlan(context, commitData,new CallBack<StringBean> (){
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    Utils.showToast(context,response.getMessage());
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }

    public void indexContractInfo(final Context context, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put(KEY_PHONE,SPUtils.get(context, SPConfig.PHONE,"").toString());
        contractModel.indexContractInfo(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    if (tag instanceof ProgramContractBean.Data){
                        ProgramContractBean.Data data = (ProgramContractBean.Data) tag;
                        listener.onLoad(data);
                        String param = "contractCode=" + data.getContractCode() + "&signer=" + data.getSigner1();
                        ((Activity)context).startActivityForResult(new Intent(context, WebActivity.class)
                            .putExtra(KEY_TYPE,WEB_TYPE_CONTRACT).putExtra(KEY_PARAM,param)
                        ,2);
                    }else{
                        Utils.showToast(context,"签署合同失败");
                    }
                }else{
                    Utils.showToast(context,"签署合同失败");
                }
            }

            @Override
            public void onFailure(String message) {
                Utils.showToast(context,"签署合同失败");
            }
        });
    }
    //方案选择商品列表
    public void selectGoodsList(Context context, String s) {
        mView.showProgress();
        Map<String,Object> params = new HashMap<>();
        params.put(KEY_PAGE, 0);
        params.put(KEY_PAGE_SIZE, DEFAULT_PAGE_SIZE);
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",SPUtils.get(context,SPConfig.ROLE_ID,"").toString());
        params.put(KEY_PARAM,map);
        goodsModel.resetGoodsList(context,params,new ResetGoodsListCallBack());

    }

    public void checkCustomerIsOrder(final Context context, String ppId, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("planId",ppId);
        if (mView == null) return;
        mView.showProgress();
        caseModel.checkOrderIsCommit(context, param, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    if (response.getData().contains("false")){
                        listener.onLoad(null);
                    }else{
                        Utils.showToast(context,"用户已下单，不可再推送。若想有追加，工程里增项。");
                    }
                }else{
                    Utils.showToast(context,"网络错误");
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context,"网络错误");
            }
        });
    }

    private class ResetGoodsListCallBack implements CallBack<com.sanjiahantang.youjiarole.module.goods.beans.GoodsListBean>{

        @Override
        public void onSuccess(com.sanjiahantang.youjiarole.module.goods.beans.GoodsListBean response, Context context, Object tag) {
            mView.hideProgress();
            listData.clear();
            listData.addAll(response.getData().getList());
            if(listMoreAdapter == null){
                listMoreAdapter = new GoodsListMoreAdapter(context,listData,null,null,null);
                Object[] obj = new Object[2];
                obj[0] = CALLBACK_TYPE_RECYCLER_ADAPTER;
                obj[1] = listMoreAdapter;
                onLoadListener.onLoad(obj);
            }
//            listMoreAdapter = new GoodsListMoreAdapter(context,listData,null);
//            onLoadListener.initRecyclerView(listMoreAdapter);
//            listMoreAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }
}
